/**
 * Copyright 2007-2008 Vidar Svansson
 *     
 * This file is part of JAdapter.
 * 
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jadapter.adapters;

import org.jadapter.Transformer;

/**
 * A template for the {@link org.jadapter.Adapter} interface.
 * This class can adapt {@link Transformer} or the transform template method can
 * be overridden. 
 * 
 * @author Vidar Svansson
 *
 * @param <T> The type this adapter adapts to.
 * @param <F> The type this adapter adapts from.
 */
public class SimpleAdapter<T,F> extends AdapterBean<T,F> {

    /**
     * A shortcut to define the result of the {@link org.jadapter.Adapter#to()} method. 
     */
    protected Class<T> to;
    
    /**
     * A shortcut to define the result of the {@link from} method. 
     */
    protected Class<F> from;
    
    private Transformer<T,F> transformer;
    
    /**
     * The empty constructor requires {@link org.jadapter.Transformer#transform } to be overridden.
     * 
     */
    public SimpleAdapter(){
        
    }
    
    /**
     * Adapts {@link org.jadapter.Transformer} to {@link org.jadapter.Adapter}.
     * @param transformer The context to adapt 
     */
    public SimpleAdapter(final Transformer<T,F> transformer){
        this.transformer = transformer;
    }    
    
    @Override public T transform(F context) {
        if(transformer != null)
            return transformer.transform(context);
        throw new UnsupportedOperationException
            ("transform is not implemented by this adapter");
    }

    @Override public Class<F> from() {
        return from == null ? super.from() : from ;
    }

    @Override public Class<T> to() {
        return to == null ? super.to() : to ;
    }
    
}
